package edu.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.school.entity.*;
import edu.school.mapper.CommentMapper;
import edu.school.mapper.ReplyMapper;
import edu.school.mapper.TieziMapper;
import edu.school.mapper.UserMapper;
import edu.school.service.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper mapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    TieziMapper tieziMapper;
    @Autowired
    ReplyMapper replyMapper;
    @Override
    public boolean save(Comment user) {
        return mapper.insertSelective(user)>0?true:false;
    }

    @Override
    public boolean update(Comment user) {
        return mapper.updateByPrimaryKeySelective(user)>0?true:false;
    }

    @Override
    public PageInfo<Comment> getPageList(Integer pageNum, Integer pageSize, Comment record) {
        CommentExample example = new CommentExample();
        example.setOrderByClause("create_time desc");
        CommentExample.Criteria criteria = example.createCriteria();
        if (null != record) {
            if (StringUtils.isNotBlank(record.getContent())) {
                criteria.andContentLike("%"+record.getContent()+"%");
            }
            if (record.getUid() != null) {
                criteria.andUidEqualTo(record.getUid());
            }

        }
        PageHelper.startPage(pageNum, pageSize);//第1步
        List<Comment> list = mapper.selectByExample(example);//第2步
        for (Comment date : list) {
            User user = userMapper.selectByPrimaryKey(date.getUid());
            date.setUser(user);
            Tiezi tiezi = tieziMapper.selectByPrimaryKey(date.getTid());
            date.setTiezi(tiezi);
        }
        PageInfo<Comment> info = new PageInfo<>(list);
        info.setPageNum(info.getPageNum());
        info.setPages(info.getPages());
        return info;

    }

    @Override
    public Comment findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteComment(Integer id) {
        return mapper.deleteByPrimaryKey(id)>0?true:false;
    }

    @Override
    public int deleteMany(String[] ids) {
        return mapper.deleteMany(ids);
    }

    @Override
        public List<Comment> findAll(Comment record) {
            CommentExample example = new CommentExample();
            example.setOrderByClause("create_time desc");
            CommentExample.Criteria criteria = example.createCriteria();
            criteria.andTidEqualTo(record.getTid());
            List<Comment> list = mapper.selectByExample(example);
            for(Comment data:list) {
                User user=userMapper.selectByPrimaryKey(data.getUid());
                data.setUser(user);
                Tiezi tiezi=tieziMapper.selectByPrimaryKey(data.getTid());
                data.setTiezi(tiezi);

                ReplyExample example2 = new ReplyExample();
                example2.setOrderByClause("create_time desc");
                ReplyExample.Criteria criteria2 = example2.createCriteria();
                criteria2.andCidEqualTo(data.getId());
                List<Reply> replyList = replyMapper.selectByExample(example2);
                for(Reply reply:replyList) {
                    User user1=userMapper.selectByPrimaryKey(reply.getUid());
                    reply.setUser(user1);
                    Comment comment=mapper.selectByPrimaryKey(reply.getCid());
                    reply.setComment(comment);
                }
                data.setReplyList(replyList);
            }
            return list;
        }
}
